<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>SSR 渲染 之后端 | 北城韩雨</title><meta name="keywords" content="Nuxt"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="Nuxt">
<meta property="og:type" content="article">
<meta property="og:title" content="SSR 渲染 之后端">
<meta property="og:url" content="http://example.com/2020/04/14/%E5%90%8E%E7%AB%AF/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="Nuxt">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f">
<meta property="article:published_time" content="2020-04-13T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="服务端渲染">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/14/%E5%90%8E%E7%AB%AF/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'SSR 渲染 之后端',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">SSR 渲染 之后端</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-04-13T16:00:00.000Z" title="发表于 2020-04-14 00:00:00">2020-04-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">项目实战</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">8.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>34分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SSR 渲染 之后端"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="1-初始化"><a href="#1-初始化" class="headerlink" title="1.初始化"></a>1.初始化</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们使用egg创建了后端项目</span><br><span class="line"><span class="number">2.</span>创建命令</span><br><span class="line">npm init egg --type=simple 初始化项目</span><br><span class="line">npm i 安装egg</span><br></pre></td></tr></table></figure>
<h1 id="2-项目结构目录"><a href="#2-项目结构目录" class="headerlink" title="2.项目结构目录"></a>2.项目结构目录</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>app文件夹</span><br><span class="line">controller 文件夹 里面存放业务层文件</span><br><span class="line">middlware  文件夹 里面存放中间件文件 比如我们在访问某个路由的时候需要token </span><br><span class="line">model 文件夹 里面存放数据模型文件 比如数据表之类的字段都可以在这里定义</span><br><span class="line">pulic 文件夹 里面存放静态资源</span><br><span class="line">service 文件夹 里面存放我们可以在全局使用的方法 暂时这么理解</span><br><span class="line">router.js 文件 里面是我们路由</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">router.js 文件代码 比如如下 </span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">//这里是把app结构出来了 我们可以拿到router 和你的 controller里面的文件 你也可以通过app点的形式 拿到那些你的结构目录下面的文件</span></span><br><span class="line">  <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line">  <span class="keyword">const</span> jwt = app.middleware.jwt(&#123; app &#125;);</span><br><span class="line">  router.get(<span class="string">&#x27;/&#x27;</span>, controller.home.index);</span><br><span class="line">  router.get(<span class="string">&#x27;/user/info&#x27;</span>, controller.user.index);</span><br><span class="line">  <span class="comment">// 发送手机验证码</span></span><br><span class="line">  router.get(<span class="string">&#x27;/user/sendcode&#x27;</span>, controller.user.email);</span><br><span class="line">  <span class="comment">// 生成图片验证码</span></span><br><span class="line">  router.get(<span class="string">&#x27;/user/captcha&#x27;</span>, controller.user.captcha);</span><br><span class="line">  <span class="comment">// 注册</span></span><br><span class="line">  router.post(<span class="string">&#x27;/user/register&#x27;</span>, controller.user.create);</span><br><span class="line">  <span class="comment">// 登录</span></span><br><span class="line">  router.post(<span class="string">&#x27;/user/login&#x27;</span>, controller.user.login);</span><br><span class="line">  <span class="comment">// test测试数据</span></span><br><span class="line">  router.get(<span class="string">&#x27;/user/detail&#x27;</span>, jwt, controller.user.detail);</span><br><span class="line">  <span class="comment">// 新建文章</span></span><br><span class="line">  router.post(<span class="string">&#x27;/article/create&#x27;</span>, jwt, controller.article.create);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">2.</span>config 文件夹</span><br><span class="line">config.default.js  文件 里面主要是 配置一些插件的配置 比如我们的mongoose 数据库的链接 jwt的配置</span><br><span class="line">plugins.js 文件  里面主要是要写我们一些加载插件的代码 比如你的数据库mongoose jwt 插件</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">config.default.js 文件 我们的代码主要是卸载末尾的<span class="keyword">return</span>里</span><br><span class="line"><span class="comment">//下面就是我们配置了mongoose 和 jwt</span></span><br><span class="line"><span class="keyword">return</span> &#123;</span><br><span class="line">    ...config,</span><br><span class="line">    ...userConfig,</span><br><span class="line">    security: &#123;</span><br><span class="line">      csrf: &#123;</span><br><span class="line">        enable: <span class="literal">false</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 配置mongoose</span></span><br><span class="line">    mongoose: &#123;</span><br><span class="line">      client: &#123;</span><br><span class="line">        url: <span class="string">&#x27;mongodb://127.0.0.1:27017/test&#x27;</span>,</span><br><span class="line">        options: &#123;</span><br><span class="line"></span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 配置jwt</span></span><br><span class="line">    jwt: &#123;</span><br><span class="line">      secret: <span class="string">&#x27;wangzhengjwt&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">plugins.js 主要就是我们要把加载那些插件的代码卸载这里 最后到导出去 比如我们的mongoose 和jwt</span><br><span class="line"><span class="comment">//写法如下</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="comment">// 集成mongoose插件</span></span><br><span class="line"><span class="built_in">exports</span>.mongoose = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">&#x27;egg-mongoose&#x27;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 集成jwt插件</span></span><br><span class="line"><span class="built_in">exports</span>.jwt = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">&#x27;egg-jwt&#x27;</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3.</span>不论怎样的写法 我们最后都要导出这个模块 也就是语法上要加</span><br><span class="line"><span class="built_in">module</span>.exports = xxx</span><br></pre></td></tr></table></figure>
<h1 id="3-路由和controller写法"><a href="#3-路由和controller写法" class="headerlink" title="3.路由和controller写法"></a>3.路由和controller写法</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们前端访问后端的时候 就需要在router.js文件里写</span><br><span class="line"><span class="number">2.</span>第一个参数是你前端访问的接口,第二个参数里面可以写中间件 你要在里面写一些规则 第三个参数 是当我们访问这个接口的时候 需要走的逻辑代码</span><br><span class="line">router.get(<span class="string">&#x27;/user/info&#x27;</span>, controller.user.index);</span><br><span class="line"><span class="number">3.</span>以上可以理解为 我们在访问/user/info这个接口的时候 走的逻辑是controller文件夹下的user.js文件里的index方法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">4.</span>为此我们需要在controller文件夹下新建一个user.js文件 里面要有一个index方法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//controller文件加下的user.js文件  写法如下</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="comment">//首先要把egg导入进来 然后加载他的Controller层</span></span><br><span class="line"><span class="keyword">const</span> Controller = <span class="built_in">require</span>(<span class="string">&#x27;egg&#x27;</span>).Controller;</span><br><span class="line"><span class="comment">//然后我们用类的写法集成我们的egg.Controller</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">  <span class="comment">//这里都是async 和 await的写法</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">index</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">//把我的this解构出来</span></span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">//这里就是把数据返回给了前端 是 hi,egg</span></span><br><span class="line">    ctx.body = <span class="string">&#x27;hi, egg&#x27;</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = HomeController;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5.</span>有的时候前端访问成功了 我们需要给他返回成功的信息 有的时候前端访问失败了 我们需要给他返回失败的信息 有的时候我们还需要提醒</span><br><span class="line">所以这个时候 我们要把上面的封装一下 然乎每个函数里能使用下面的方法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先在controller下新建一个base.js 这里主要是要把成功 失败 提示 的方法都封装一下</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> Controller = <span class="built_in">require</span>(<span class="string">&#x27;egg&#x27;</span>).Controller;</span><br><span class="line"><span class="comment">//下面就是我们封装的方法 </span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="title">message</span>(<span class="params">message</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.ctx.body = &#123;</span><br><span class="line">      code: <span class="number">0</span>,</span><br><span class="line">      message,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// @ 公用代码抽离</span></span><br><span class="line">  <span class="function"><span class="title">success</span>(<span class="params">data</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.ctx.body = &#123;</span><br><span class="line">      code: <span class="number">0</span>,</span><br><span class="line">      data,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">error</span>(<span class="params">message, code = -<span class="number">1</span></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">this</span>.ctx.body = &#123;</span><br><span class="line">      code,</span><br><span class="line">      message,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//最后把他导出</span></span><br><span class="line"><span class="built_in">module</span>.exports = BaseController;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//然后我们在其他的controller文件夹里新建的文件 都导入上面的文件 然后通过集成的方法 使用他里面的三个方法</span></span><br><span class="line"><span class="comment">//用user.js文件里举例 写法如下</span></span><br><span class="line"><span class="comment">//导入我们写好的 </span></span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="comment">//然后继承</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">index</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">//这里就是调用了上面我们封装的方法</span></span><br><span class="line">	<span class="built_in">this</span>.success(<span class="string">&#x27;返回的测试数据&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125; </span><br><span class="line"><span class="built_in">module</span>.exports = UserController</span><br></pre></td></tr></table></figure>
<h1 id="4-给邮箱发送验证码"><a href="#4-给邮箱发送验证码" class="headerlink" title="4.给邮箱发送验证码"></a>4.给邮箱发送验证码</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们使用第三方的库 nodemailer 所以我们需要安装一下</span><br><span class="line"><span class="number">2.</span>这里应为是公共的方法 所以我们需要新建service的文件夹 在新建一个js文件</span><br><span class="line"><span class="number">3.</span>我们使用这个公共的方法直接就可以在controller层中使用<span class="built_in">this</span>.service.新建的js文件名称.方法即可</span><br><span class="line"><span class="number">4.</span>这里需要注意 你要给用户发送验证码的邮箱需要在设置账户里开启POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务才可以</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//controller层逻辑</span></span><br><span class="line">首先在router.js文件里写上接口</span><br><span class="line">router.get(<span class="string">&#x27;/user/sendcode&#x27;</span>, controller.user.email);</span><br><span class="line">controller文件夹 user.js文件 写处理函数</span><br><span class="line"><span class="comment">//这里我们继承了我们基本的成功请求 失败 和信息 详情见上面</span></span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 给邮箱发送验证码</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">email</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">//接收前台传过来的邮箱</span></span><br><span class="line">    <span class="keyword">const</span> email = ctx.query.email;</span><br><span class="line">    <span class="comment">//生成验证码  </span></span><br><span class="line">    <span class="keyword">const</span> code = <span class="built_in">Math</span>.random()</span><br><span class="line">      .toString()</span><br><span class="line">      .slice(<span class="number">2</span>, <span class="number">6</span>);</span><br><span class="line">	<span class="comment">//编写给你邮箱发送的内容  邮箱里可以写html格式</span></span><br><span class="line">    <span class="keyword">const</span> title = <span class="string">&#x27;开课吧验证码&#x27;</span>;</span><br><span class="line">    <span class="keyword">const</span> html = <span class="string">`</span></span><br><span class="line"><span class="string">      &lt;h1&gt;开课吧注册验证码&lt;/h1&gt;</span></span><br><span class="line"><span class="string">      &lt;div&gt;</span></span><br><span class="line"><span class="string">        &lt;a href=&quot;https://kaikeba.com/&quot;&gt;<span class="subst">$&#123;code&#125;</span>&lt;/a&gt;</span></span><br><span class="line"><span class="string">      &lt;/div&gt;</span></span><br><span class="line"><span class="string">    `</span>;</span><br><span class="line">    <span class="comment">//我们在service的文件夹下新建了一个tool的js文件里面有sendEmail方法  这里就需要用到 nodemailer 记得安装</span></span><br><span class="line">    <span class="keyword">const</span> hasSend = <span class="keyword">await</span> <span class="built_in">this</span>.service.tools.sendEmail(email, title, html);</span><br><span class="line">    <span class="comment">//如果我们哪个第三方发送邮件的方法成功了就会给我们返回一个值  </span></span><br><span class="line">    <span class="keyword">if</span> (hasSend) &#123;</span><br><span class="line">      <span class="comment">//然后我们把我们写好的验证码存放在session当中  </span></span><br><span class="line">      ctx.session.emailcode = code;</span><br><span class="line">      <span class="comment">//返还给前端消息 说发送成功  </span></span><br><span class="line">      <span class="built_in">this</span>.message(<span class="string">&#x27;发送成功&#x27;</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">//如果失败了 返还给前端消息说发送失败  </span></span><br><span class="line">      <span class="built_in">this</span>.error(<span class="string">&#x27;发送失败&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//nodemailer 配置 首先新建service文件夹 -&gt; tools.js文件 然后我们在controller层中就可以通过this.service.tools.方法调用了</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="comment">//首先要加载我们service这个项</span></span><br><span class="line"><span class="keyword">const</span> Service = <span class="built_in">require</span>(<span class="string">&#x27;egg&#x27;</span>).Service;</span><br><span class="line"><span class="comment">// 这个是自动给你的邮箱发送验证码的模块 加载</span></span><br><span class="line"><span class="keyword">const</span> nodemailer = <span class="built_in">require</span>(<span class="string">&#x27;nodemailer&#x27;</span>);</span><br><span class="line"><span class="comment">//配置你要用哪个邮箱去给你的客户端邮箱发送验证码</span></span><br><span class="line"><span class="keyword">const</span> userEmail = <span class="string">&#x27;1052101962@qq.com&#x27;</span>;</span><br><span class="line"><span class="comment">//这些是 nodemailer 一些基本配置 比如邮箱是什么端口少之类的 你要用哪个邮箱去给客户端邮箱发送</span></span><br><span class="line"><span class="keyword">const</span> transporter = nodemailer.createTransport(&#123;</span><br><span class="line">  service: <span class="string">&#x27;qq&#x27;</span>,</span><br><span class="line">  port: <span class="number">465</span>,</span><br><span class="line">  secureConnetion: <span class="literal">true</span>,</span><br><span class="line">  auth: &#123;</span><br><span class="line">    user: userEmail,</span><br><span class="line">    pass: <span class="string">&#x27;xmzoiyzsdkbrbdfg&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//下面是我们公共类的写法 一定要继承egg的Service 然后里面是方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ToolsService</span> <span class="keyword">extends</span> <span class="title">Service</span> </span>&#123;</span><br><span class="line">  <span class="comment">//这里就是我们在controller层调用的那个函数 把前端传过来的邮箱传了进去 然后 我们还写了一些title和html片段  </span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">sendEmail</span>(<span class="params">email, title, html</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> mailOptions = &#123;</span><br><span class="line">      <span class="keyword">from</span>: userEmail,</span><br><span class="line">      to: email,</span><br><span class="line">      subject: title,</span><br><span class="line">      text: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">      html,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//尝试着用nodemailer 去发送邮件  </span></span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="comment">//如果成功了就返回true 我们controller层就能做出对应的判断  </span></span><br><span class="line">      <span class="keyword">await</span> transporter.sendMail(mailOptions);</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (err) &#123;</span><br><span class="line">      <span class="comment">//失败了就返回false   </span></span><br><span class="line">      <span class="built_in">console</span>.log(err);</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = ToolsService</span><br></pre></td></tr></table></figure>
<h1 id="5-图片验证码"><a href="#5-图片验证码" class="headerlink" title="5.图片验证码"></a>5.图片验证码</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>在前端注册时候我们 后面也会有一个图片验证码 或者图片滑动的</span><br><span class="line"><span class="number">2.</span>这里后端用第三方的包去 实现 图片验证码 svg-captcha</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先前端是个img标签 然后 给他的src属性 写我们的图片验证接口</span></span><br><span class="line">router.get(<span class="string">&#x27;/user/captcha&#x27;</span>, controller.user.captcha);</span><br><span class="line"><span class="comment">//访问的是我们controller文件夹下的user.js文件里面的captcha方法</span></span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">captcha</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">// 调用了service的文件夹里tolls的方法 生成了随机的图片验证码</span></span><br><span class="line">    <span class="keyword">const</span> captcha = <span class="keyword">await</span> <span class="built_in">this</span>.service.tools.captcha();</span><br><span class="line">    <span class="comment">// 把后台刚刚生成的随机验证码放在了session里</span></span><br><span class="line">    ctx.session.captcha = captcha.text;</span><br><span class="line">    <span class="comment">// 设置了响应头的类型</span></span><br><span class="line">    ctx.response.type = <span class="string">&#x27;image/svg+xml&#x27;</span>;</span><br><span class="line">    <span class="comment">// 然后把数据传送了回去</span></span><br><span class="line">    ctx.body = captcha.data;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">service文件夹下的tools.js文件 captcha方法</span><br><span class="line"><span class="comment">//首先要加载 svg-captcha 第三方包</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> Service = <span class="built_in">require</span>(<span class="string">&#x27;egg&#x27;</span>).Service;</span><br><span class="line"><span class="comment">// 这个是生成图片验证码的模块</span></span><br><span class="line"><span class="keyword">const</span> svgCaptcha = <span class="built_in">require</span>(<span class="string">&#x27;svg-captcha&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ToolsService</span> <span class="keyword">extends</span> <span class="title">Service</span> </span>&#123;</span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">captcha</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">//配置了一下生成的图片验证码  </span></span><br><span class="line">    <span class="keyword">const</span> captcha = svgCaptcha.create(&#123;</span><br><span class="line">      size: <span class="number">4</span>,</span><br><span class="line">      fontSize: <span class="number">50</span>,</span><br><span class="line">      width: <span class="number">100</span>,</span><br><span class="line">      height: <span class="number">40</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">return</span> captcha;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = ToolsService</span><br><span class="line">;</span><br></pre></td></tr></table></figure>
<h1 id="6-后端注册逻辑"><a href="#6-后端注册逻辑" class="headerlink" title="6.后端注册逻辑"></a>6.后端注册逻辑</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们最开始的时候 邮箱验证码 和图片验证码都存放在session里了</span><br><span class="line"><span class="number">2.</span>当点击注册时候 我们首先去数据库里查看 邮箱是否重名了</span><br><span class="line"><span class="number">3.</span>如果没重名 我们要看 你的邮箱验证码和图片验证码 跟我们在session里存放的一样一样</span><br><span class="line"><span class="number">4.</span>如果以上都可以了 我们就要进行入库操作 密码需要用md5加密</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当前端点击注册时候 我们要写接口路由</span></span><br><span class="line">router.post(<span class="string">&#x27;/user/register&#x27;</span>, controller.user.create);</span><br><span class="line"><span class="comment">//controller文件夹下 user.js文件 下的create方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="comment">//加载md5 注册的时候我们要给密码加密</span></span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="comment">//这个是查看数据库里有没有这个邮箱 具体数据库设置我们下面在看</span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">checkEmail</span>(<span class="params">email</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> user = <span class="keyword">await</span> <span class="built_in">this</span>.ctx.model.User.findOne(&#123; email &#125;);</span><br><span class="line">    <span class="keyword">return</span> user;</span><br><span class="line">  &#125;</span><br><span class="line">    <span class="comment">//注册逻辑</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">create</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">//首先把前端给我们传送过来的参数都解构成我们数据库的字段</span></span><br><span class="line">    <span class="keyword">const</span> &#123; email, password, emailcode, captcha, nickname &#125; = ctx.request.body;</span><br><span class="line">    <span class="comment">//如果你前端发送过来的邮箱验证码和我们session里的不一样 就告诉他出错了  </span></span><br><span class="line">    <span class="keyword">if</span> (emailcode !== ctx.session.emailcode) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">this</span>.error(<span class="string">&#x27;邮箱验证码出错&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//如果你前端发送过来的图片验证码转换成小写之后 和我们session中存放的图片验证码不一样就告诉他错误</span></span><br><span class="line">    <span class="keyword">if</span> (captcha.toUpperCase() !== ctx.session.captcha.toUpperCase()) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">this</span>.error(<span class="string">&#x27;图片验证码错误&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//这里我们调用了上面去库里查看邮箱有没有存在的方法  </span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">await</span> <span class="built_in">this</span>.checkEmail(email)) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">this</span>.error(<span class="string">&#x27;邮箱已经存在&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//然后执行入库操作  </span></span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> ctx.model.User.create(&#123;</span><br><span class="line">      email,</span><br><span class="line">      nickname,</span><br><span class="line">      <span class="comment">//这里使用md5把密码加密  </span></span><br><span class="line">      password: md5(password),</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//如果入库操作给你返回ID了 就说明注册成功了  </span></span><br><span class="line">    <span class="keyword">if</span> (ret._id) &#123;</span><br><span class="line">      <span class="built_in">this</span>.success(<span class="string">&#x27;注册成功&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="7-数据库配置"><a href="#7-数据库配置" class="headerlink" title="7.数据库配置"></a>7.数据库配置</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们用mongodb 的插件 mongoose</span><br><span class="line"><span class="number">2.</span>因为这是插件 所以我们需要在项目目录的config文件夹下的plugins.js文件先加载 还有我的jwt也是如此</span><br><span class="line"><span class="number">3.</span>然后我们需要在 config文件夹下的config.defualt.js文件里 做配置</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//config文件夹下config.defualt.js文件 return里写我们的配置代码</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">appInfo</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> config = (<span class="built_in">exports</span> = &#123;&#125;);</span><br><span class="line">  config.keys = appInfo.name + <span class="string">&#x27;_1582961099075_5945&#x27;</span>;</span><br><span class="line">  config.middleware = [];</span><br><span class="line">  <span class="keyword">const</span> userConfig = &#123;</span><br><span class="line">  &#125;;</span><br><span class="line">  <span class="comment">//这里我们就需要在return里写下配置即可	</span></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    ...config,</span><br><span class="line">    ...userConfig,</span><br><span class="line">    security: &#123;</span><br><span class="line">      csrf: &#123;</span><br><span class="line">        enable: <span class="literal">false</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 配置mongoose </span></span><br><span class="line">    mongoose: &#123;</span><br><span class="line">      client: &#123;</span><br><span class="line">        <span class="comment">//你要链接mongodb下的哪个数据库  </span></span><br><span class="line">        url: <span class="string">&#x27;mongodb://127.0.0.1:27017/test&#x27;</span>,</span><br><span class="line">        options: &#123;</span><br><span class="line"></span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 配置jwt</span></span><br><span class="line">    jwt: &#123;</span><br><span class="line">      secret: <span class="string">&#x27;wangzhengjwt&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//config文件夹下的plugins.js 用于加载我们的插件 可能是固定语法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="comment">// 加载mongoose插件</span></span><br><span class="line"><span class="built_in">exports</span>.mongoose = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">&#x27;egg-mongoose&#x27;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 加载jwt插件</span></span><br><span class="line"><span class="built_in">exports</span>.jwt = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">&#x27;egg-jwt&#x27;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="8-mongoose的使用"><a href="#8-mongoose的使用" class="headerlink" title="8.mongoose的使用"></a>8.mongoose的使用</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>通过以上文件我们就配置好了数据库 </span><br><span class="line"><span class="number">2.</span>我们要新建一个model的文件夹 然后新建js文件 这个js文件就是你的数据表 你可以在这个js文件里面定义字段</span><br><span class="line"><span class="number">3.</span>当我们使用的时候 直接就可以在controller层里使用ctx.model.User来执行数据库命令</span><br><span class="line"><span class="number">4.</span>这里我们用用户信息来举例</span><br><span class="line"><span class="number">5.</span>记得要安装egg-mongoose</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//app文件夹下model文件夹下的user.js文件 </span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">//一些基本配置  </span></span><br><span class="line">  <span class="keyword">const</span> mongoose = app.mongoose;</span><br><span class="line">  <span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line">  <span class="comment">//定义了一些字段你要传入的   </span></span><br><span class="line">  <span class="keyword">const</span> UserSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">    <span class="comment">// select就是在我们查询数据库的时候不返还给前端的设置</span></span><br><span class="line">    __v: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    email: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    password: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    nickname: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">  &#125;, &#123; <span class="attr">timestamps</span>: <span class="literal">true</span> &#125;);</span><br><span class="line">  <span class="comment">//记得这里一定要return获取 第一个参数是你的表的名字 至于为什么大写我也不知道</span></span><br><span class="line">  <span class="keyword">return</span> mongoose.model(<span class="string">&#x27;User&#x27;</span>, UserSchema);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当我们写好了以上的配置之后 我们就可以在controller层中的方法里调用我们数据库的语法了 这里以我们第6个注册逻辑来查看邮箱是否存在距离</span></span><br><span class="line">controller文件夹下的user.js文件</span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span></span>&#123;</span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">checkEmail</span>(<span class="params">email</span>)</span> &#123;</span><br><span class="line">    <span class="comment">//这里就是用this.ctx.model(文件夹).User(数据表)去找有没有重名的email    </span></span><br><span class="line">    <span class="keyword">const</span> user = <span class="keyword">await</span> <span class="built_in">this</span>.ctx.model.User.findOne(&#123; email &#125;);</span><br><span class="line">    <span class="keyword">return</span> user;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="9-jwt后端登录token权限"><a href="#9-jwt后端登录token权限" class="headerlink" title="9.jwt后端登录token权限"></a>9.jwt后端登录token权限</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们现在的项目是先后端分离 就没有session那一个套权限</span><br><span class="line"><span class="number">2.</span>现在都改为是的token 相比原来的技术 就是拓展性更强 客户端方便会轻松一些</span><br><span class="line"><span class="number">3.</span>我们每个请求头里都会带着token去后台访问接口</span><br><span class="line"><span class="number">4.</span>这里我们使用生成token的第三包就是egg-jwt</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由接口</span></span><br><span class="line">router.post(<span class="string">&#x27;/user/login&#x27;</span>, controller.user.login);</span><br><span class="line"><span class="comment">//controller文件夹下user.js文件 login接口</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="comment">//这里需要md5加密一下</span></span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 登录</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">login</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx, app &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">// 接收前台传过来的参数</span></span><br><span class="line">    <span class="keyword">const</span> &#123; email, password &#125; = ctx.request.body;</span><br><span class="line">    <span class="comment">// 然后去User的表里查询 具体相关配置查看上面</span></span><br><span class="line">    <span class="keyword">const</span> user = <span class="keyword">await</span> ctx.model.User.findOne(&#123;</span><br><span class="line">      email,</span><br><span class="line">      password: md5(password),</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//如果查到了 我们就生成token  </span></span><br><span class="line">    <span class="keyword">if</span> (user) &#123;</span><br><span class="line">      <span class="keyword">const</span> &#123; nickname &#125; = user;</span><br><span class="line">      <span class="comment">//这里就是用jwt插件 需要在config.defualt.js配置 和 plugins.js去配置 具体查看上面 </span></span><br><span class="line">      <span class="comment">//这个插件会根据 我们的邮箱和ID 还有名称生成token令牌  </span></span><br><span class="line">      <span class="keyword">const</span> token = app.jwt.sign(&#123;</span><br><span class="line">        nickname,</span><br><span class="line">        email,</span><br><span class="line">        id: user._id,</span><br><span class="line">      &#125;, app.config.jwt.secret, &#123;</span><br><span class="line">        expiresIn: <span class="string">&#x27;1h&#x27;</span>,</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="comment">//然后返还给前端  </span></span><br><span class="line">      <span class="built_in">this</span>.success(&#123; token, email &#125;);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.error(<span class="string">&#x27;用户名或者密码错误&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="10-解密token"><a href="#10-解密token" class="headerlink" title="10.解密token"></a>10.解密token</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>上面我们设置了token 以后我们只要涉及到权限的接口就需要设置验证一下token</span><br><span class="line"><span class="number">2.</span>所以这里就涉及到了中间件 当我们访问后台的接口的时候 需要走你的这个中间件 然后去解密token 然后在走后面的controller里的函数</span><br><span class="line"><span class="number">3.</span>中间的设置我需要在项目目录 midderware文件夹新建js文件然后写逻辑代码</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当我们前台访问后端路由的时候 都要走中间件</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; router, controller &#125; = app;</span><br><span class="line">    <span class="comment">//这里就是调用了app文件夹下的middlerware文件夹下的jwt的方法然后把app传进去了</span></span><br><span class="line">    <span class="keyword">const</span> jwt = app.middleware.jwt(&#123; app &#125;);</span><br><span class="line">    <span class="comment">//然年 前端访问接口的时候 就要走jwt这个中间件</span></span><br><span class="line">    <span class="comment">// 新建文章</span></span><br><span class="line">  	router.post(<span class="string">&#x27;/article/create&#x27;</span>, jwt, controller.article.create);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//middleware 文件夹下 jwt.js </span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function">(<span class="params">&#123; app &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">async</span> <span class="function"><span class="keyword">function</span> <span class="title">verify</span>(<span class="params">ctx, next</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 把前端发送过来的token替换出来</span></span><br><span class="line">    <span class="keyword">const</span> token = ctx.request.header.authorization.replace(<span class="string">&#x27;Bearer &#x27;</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="comment">// 去解密token</span></span><br><span class="line">      <span class="keyword">const</span> ret = app.jwt.verify(token, app.config.jwt.secret);</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;中间件获取token信息&#x27;</span>, ret);</span><br><span class="line">      <span class="comment">// 解密出来的token里会有账户的邮箱和ID 然后把他挂在到全局</span></span><br><span class="line">      <span class="comment">// 当然这里库里的密码什么的也被查询出去了所以我们需要在model文件里user.js去设置一下不返还密码之类的字段</span></span><br><span class="line">      ctx.state.email = ret.email;</span><br><span class="line">      ctx.state.userid = ret.id;</span><br><span class="line">      <span class="keyword">await</span> next();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (err) &#123;</span><br><span class="line">      <span class="keyword">if</span> (err.name === <span class="string">&#x27;TokenExpiredError&#x27;</span>) &#123;</span><br><span class="line">        ctx.body = &#123; <span class="attr">code</span>: -<span class="number">666</span>, <span class="attr">message</span>: <span class="string">&#x27;token过期了请登录&#x27;</span> &#125;;</span><br><span class="line">        <span class="keyword">return</span> ctx.body;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">console</span>.log(err);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h1 id="11-新建文章"><a href="#11-新建文章" class="headerlink" title="11.新建文章"></a>11.新建文章</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先前端接收到的参数就是文章的内容和标题</span><br><span class="line"><span class="number">2.</span>然后我们要新建数据表 </span><br><span class="line"><span class="number">3.</span>然后存放我们的文章</span><br><span class="line"><span class="number">4.</span>这里我们前端的新建文章是经过marked处理的 所以后端需要转换一下</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 新建文章</span></span><br><span class="line">router.post(<span class="string">&#x27;/article/create&#x27;</span>, jwt, controller.article.create);</span><br><span class="line"><span class="comment">//controller文件下 我们新建article.js 层文件</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> marked = <span class="built_in">require</span>(<span class="string">&#x27;marked&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ArticleContoller</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;  </span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">create</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">//这里我们在解密的时候经过了我们的中间件 可以把ID解出来  </span></span><br><span class="line">    <span class="keyword">const</span> &#123; userid &#125; = ctx.state;</span><br><span class="line">    <span class="comment">//然后接受前端传过来的文章内容  </span></span><br><span class="line">    <span class="keyword">const</span> &#123; content &#125; = ctx.request.body;</span><br><span class="line">    <span class="comment">//处理一下  </span></span><br><span class="line">    <span class="keyword">const</span> title = content.split(<span class="string">&#x27;\n&#x27;</span>).find(<span class="function"><span class="params">v</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> v.indexOf(<span class="string">&#x27;#&#x27;</span>) === <span class="number">0</span>;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//在我们新建的文章之后 就已经把author的关联进去 关联的条件就是我们自己的ID 通过这个就可以查到作者的消息  </span></span><br><span class="line">    <span class="keyword">const</span> obj = &#123;</span><br><span class="line">      title: title.replace(<span class="string">&#x27;#&#x27;</span>, <span class="string">&#x27;&#x27;</span>),</span><br><span class="line">      article: content,</span><br><span class="line">      article_html: marked(content),</span><br><span class="line">      author: userid,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//然后存放在数据表里 这里我们在model里新建了一个表  详情见下 </span></span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> ctx.model.Article.create(obj);</span><br><span class="line">    <span class="keyword">if</span> (ret._id) &#123;</span><br><span class="line">      <span class="built_in">this</span>.success(&#123;</span><br><span class="line">        id: ret.id,</span><br><span class="line">        title: ret.title,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.error(<span class="string">&#x27;新建失败&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = ArticleContoller;</span><br></pre></td></tr></table></figure>
<h1 id="12-article表"><a href="#12-article表" class="headerlink" title="12.article表"></a>12.article表</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们的文章是要存放在我们的aticle表里</span><br><span class="line"><span class="number">2.</span>所以需要在model新建aticle表 设计字段</span><br><span class="line"><span class="number">3.</span>这涉及到多表查询</span><br><span class="line"><span class="number">4.</span>我们每个文章的表 都要有作者的信息 所以这里就要关联到我们自己的账号一些属性</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//model文件夹下 article.js文件 </span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> mongoose = app.mongoose;</span><br><span class="line">  <span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line">  <span class="keyword">const</span> UserSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">    <span class="comment">// select就是在我们查询数据库的时候不返还给前端的设置</span></span><br><span class="line">    __v: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    title: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    article: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    article_html: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    views: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    author: &#123;</span><br><span class="line">      type: Schema.Types.ObjectId,</span><br><span class="line">      <span class="comment">//这里的ref 就是关联到我们自己的账号信息了 返回前端的数据的时候 会把我们自己的账号信息也直接返还回去</span></span><br><span class="line">      ref: <span class="string">&#x27;User&#x27;</span>,</span><br><span class="line">      required: <span class="literal">true</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">     <span class="comment">// 文章的赞数</span></span><br><span class="line">    like: &#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">required</span>: <span class="literal">false</span>, <span class="attr">defualt</span>: <span class="number">0</span> &#125;,</span><br><span class="line">    <span class="comment">// 文章的踩数</span></span><br><span class="line">    dilike: &#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">required</span>: <span class="literal">false</span>, <span class="attr">defualt</span>: <span class="number">0</span> &#125;,</span><br><span class="line">    <span class="comment">// 上面两者是互斥的</span></span><br><span class="line">  &#125;, &#123; <span class="attr">timestamps</span>: <span class="literal">true</span> &#125;);</span><br><span class="line">  <span class="keyword">return</span> mongoose.model(<span class="string">&#x27;Article&#x27;</span>, UserSchema);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="13-获取文章列表"><a href="#13-获取文章列表" class="headerlink" title="13.获取文章列表"></a>13.获取文章列表</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先前端会有一个页面 展示所有的文章</span><br><span class="line"><span class="number">2.</span>前端发送请求 我们去Article的表里去查出来素有的文章</span><br><span class="line"><span class="number">3.</span>这里查出来的数据 我们在新建的时候 就通过author字段 把我们作者的信息也创建进去了 条件是ID</span><br><span class="line"><span class="number">4.</span>所以我们在查询的时候需要关联author字段 然后把作者的表也查进去</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取文章</span></span><br><span class="line">router.get(<span class="string">&#x27;/article&#x27;</span>, controller.article.index);</span><br><span class="line"><span class="comment">//controller文件夹下的 article.js index方法</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="title">index</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">//这就是去我们的文章表里 去查找到所有的文章 然后通过populate方法 把我们的user表里的数据拿了出来</span></span><br><span class="line">    <span class="comment">//这里我们在article表的时候 设计author字段的时候 通过ref：user 就把我们的user表关联进去</span></span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> ctx.model.Article.find().populate(<span class="string">&#x27;author&#x27;</span>);</span><br><span class="line">    <span class="built_in">this</span>.success(ret);</span><br><span class="line">  &#125;</span><br><span class="line"><span class="comment">//返回返回前端的数据格式</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//这里是文章的ID</span></span><br><span class="line">   _id: <span class="string">&quot;5e69003d0250293e90108ff6&quot;</span></span><br><span class="line">    title: <span class="string">&quot; node.js基础实战&quot;</span></span><br><span class="line">    article: <span class="string">&quot;# node.js基础实战↵墨小邪&quot;</span></span><br><span class="line">    article_html: <span class="string">&quot;&lt;h1 id=&quot;</span>nodejs基础实战<span class="string">&quot;&gt;node.js基础实战&lt;/h1&gt;↵&lt;p&gt;墨小邪&lt;/p&gt;↵&quot;</span></span><br><span class="line">    <span class="comment">//这里就是我们上面所说的 通过在设计article表的时候 author字段的ref：user 就把我们user表里的数据关联过去了</span></span><br><span class="line">    author:&#123;</span><br><span class="line">    following: []</span><br><span class="line">    likeArticle: [<span class="string">&quot;5e69003d0250293e90108ff6&quot;</span>]</span><br><span class="line">    disArticle: []</span><br><span class="line">    _id: <span class="string">&quot;5e6900220250293e90108ff5&quot;</span></span><br><span class="line">    email: <span class="string">&quot;1052101962@qq.com&quot;</span></span><br><span class="line">    nickname: <span class="string">&quot;墨晓邪&quot;</span></span><br><span class="line">    createdAt: <span class="string">&quot;2020-03-11T15:13:38.012Z&quot;</span></span><br><span class="line">    updatedAt: <span class="string">&quot;2020-03-11T15:14:09.579Z&quot;</span></span><br><span class="line">    &#125;   </span><br><span class="line">    __proto__: <span class="built_in">Object</span></span><br><span class="line">    createdAt: <span class="string">&quot;2020-03-11T15:14:05.131Z&quot;</span></span><br><span class="line">    updatedAt: <span class="string">&quot;2020-03-11T15:17:25.366Z&quot;</span></span><br><span class="line">    views: <span class="number">12</span></span><br><span class="line">    like: <span class="number">2</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="14-文章详情"><a href="#14-文章详情" class="headerlink" title="14.文章详情"></a>14.文章详情</h1><h2 id="14-1-查询文章详情"><a href="#14-1-查询文章详情" class="headerlink" title="14.1 查询文章详情"></a>14.1 查询文章详情</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>前端会把文章的ID 给我们传进来 </span><br><span class="line"><span class="number">2.</span>我们根据前端传进来的文章id去article表里查询</span><br><span class="line"><span class="number">3.</span>这里我们在查的时候 就说明 有人预览了前面的文章 所以浏览量可以有一个字段 在查的时候 自己加<span class="number">1</span> </span><br><span class="line"><span class="number">4.</span>返回的数据有文章的信息 还有作者的详情 上面我们通过author&#123;<span class="attr">ref</span>:User&#125;关联 就把user表映射到文章表的author字段</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//写路由  获取具体文章</span></span><br><span class="line">router.get(<span class="string">&#x27;/article/:id&#x27;</span>, controller.article.detail);</span><br><span class="line"><span class="comment">//controller文件夹下的 article.js 的detail方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ArticleController</span> <span class="keyword">extends</span> <span class="title">BaseController</span></span>&#123;</span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">detail</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">        <span class="comment">//把前端传过来的文章ID搞出来</span></span><br><span class="line">        <span class="keyword">const</span> &#123; id &#125; = ctx.params;</span><br><span class="line"><span class="comment">//根据文章ID去查指定文章 然后找到一次就用findOneAndUpdate方法 让views 增加一次 然后把作者的详细信息映射到查到的结果里</span></span><br><span class="line">        <span class="keyword">const</span> info = <span class="keyword">await</span> ctx.model.Article.findOneAndUpdate(&#123; <span class="attr">_id</span>: id &#125;, &#123; <span class="attr">$inc</span>: &#123; <span class="attr">views</span>: <span class="number">1</span> &#125; &#125;).populate(<span class="string">&#x27;author&#x27;</span>);</span><br><span class="line">        <span class="built_in">this</span>.success(info);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="14-2关注该哦片文章作者逻辑"><a href="#14-2关注该哦片文章作者逻辑" class="headerlink" title="14.2关注该哦片文章作者逻辑"></a>14.2关注该哦片文章作者逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里的关注指的的关注作者</span><br><span class="line"><span class="number">2.</span>所以我们当前的账号的用户表里 需要有一个字段 是一个数组 里面的数据是我们关注的人ID</span><br><span class="line"><span class="number">3.</span>我们通过前端传过来的数据可以获取 查看的文章的作者信息 然后去我们的follow字段路去查询看看 有没有此人 如果有就返回给前端布尔值 前端根据我们的布尔值 去显示关注的按钮的状态</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//用户表字段</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> mongoose = app.mongoose;</span><br><span class="line">  <span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> UserSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">    <span class="comment">// select就是在我们查询数据库的时候不返还给前端的设置</span></span><br><span class="line">    __v: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    email: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    password: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    nickname: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    <span class="comment">// 关注的人字段 只要你关注了 这里面就会有关注的那个人的信息</span></span><br><span class="line">    following: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;User&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line">    likeArticle: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;Article&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line">    disArticle: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;Article&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;, &#123; <span class="attr">timestamps</span>: <span class="literal">true</span> &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> mongoose.model(<span class="string">&#x27;User&#x27;</span>, UserSchema);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 看看该片文章是否关注 前端会带着正在查看该片文章的作者的ID的过来</span></span><br><span class="line">router.get(<span class="string">&#x27;/user/isfollow/:id&#x27;</span>, jwt, controller.user.isFollow);</span><br><span class="line"><span class="comment">// controller文件夹下 user.js文件 isFollow方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 是否关注了该文章文章的作者</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">isFollow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">// 前端把写文章的账号的ID传送过来了</span></span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">// 首先我们根据我们自己的ID 把我们的表里的所有的字段都查询出来</span></span><br><span class="line">    <span class="keyword">const</span> me = <span class="keyword">await</span> ctx.model.User.findById(ctx.state.userid);</span><br><span class="line">    <span class="comment">// 然后看看我们的following字段里的数据有没有我们前端传过来的ID</span></span><br><span class="line">    <span class="keyword">const</span> isFollow = !!me.following.find(<span class="function"><span class="params">v</span> =&gt;</span> v.toString() === ctx.params.id);</span><br><span class="line">    <span class="comment">// 然后返回给前端 前端会根据这个字段去判断我有没有关注该文章</span></span><br><span class="line">    <span class="built_in">this</span>.success(&#123;</span><br><span class="line">      isFollow,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="14-3关注逻辑"><a href="#14-3关注逻辑" class="headerlink" title="14.3关注逻辑"></a>14.3关注逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>前台会给我们传来该篇文章作者的ID </span><br><span class="line"><span class="number">2.</span>我们接收到以后先去用我们jwt的中间件里保存的自己的ID去我们自己的User表里查一下 返回的就是我们自己的用户表</span><br><span class="line"><span class="number">3.</span>然后在查一下following这个字段 这个字段里就存放这我们已经关注的作者的ID 然后跟前端的传过来的该片文章作者的ID匹配一下看看有没有</span><br><span class="line"><span class="number">4.</span>最后返回布尔值 前端用于判断</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由  关注该片文章</span></span><br><span class="line">router.put(<span class="string">&#x27;/user/follow/:id&#x27;</span>, jwt, controller.user.follow);</span><br><span class="line"><span class="comment">//controller文件夹 user.js follow方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 关注 该片文章作者</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">follow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">// 还是先去库里把我们自己的表的字段查询出来</span></span><br><span class="line">    <span class="keyword">const</span> me = <span class="keyword">await</span> <span class="built_in">this</span>.ctx.model.User.findById(ctx.state.userid);</span><br><span class="line">    <span class="comment">// 然后看看我们有没有关注该片文章 返回布尔值</span></span><br><span class="line">    <span class="keyword">const</span> isFollow = !!me.following.find(<span class="function"><span class="params">v</span> =&gt;</span> v.toString() === ctx.params.id);</span><br><span class="line">    <span class="comment">// 如果没有关注 我们就往我们follow的字段里 去把这个作者的ID 添加一下 也就是关注</span></span><br><span class="line">    <span class="keyword">if</span> (!isFollow) &#123;</span><br><span class="line">      me.following.push(ctx.params.id);</span><br><span class="line">      <span class="comment">// 然后保存</span></span><br><span class="line">      me.save();</span><br><span class="line">      <span class="built_in">this</span>.message(<span class="string">&#x27;关注成功&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="14-4取消关注逻辑"><a href="#14-4取消关注逻辑" class="headerlink" title="14.4取消关注逻辑"></a>14.4取消关注逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>基本上和上面一样</span><br><span class="line"><span class="number">2.</span>如果有 我们就删除following字段里的 ID</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由  关注该片文章</span></span><br><span class="line">router.delete(<span class="string">&#x27;/user/follow/:id&#x27;</span>, jwt, controller.user.unfollow);</span><br><span class="line"><span class="comment">//controller文件夹 user.js unfollow方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 取消关注</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">unfollow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="keyword">const</span> me = <span class="keyword">await</span> <span class="built_in">this</span>.ctx.model.User.findById(ctx.state.userid);</span><br><span class="line">    <span class="comment">// 还是看看能不能找到 如果找到了就删除</span></span><br><span class="line">    <span class="keyword">const</span> index = me.following.map(<span class="function"><span class="params">id</span> =&gt;</span> id.toString()).indexOf(ctx.params.id);</span><br><span class="line">    <span class="keyword">if</span> (index &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">      me.following.splice(index, <span class="number">1</span>);</span><br><span class="line">      me.save();</span><br><span class="line">      <span class="built_in">this</span>.message(<span class="string">&#x27;取消成功&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="14-5关注和粉丝逻辑"><a href="#14-5关注和粉丝逻辑" class="headerlink" title="14.5关注和粉丝逻辑"></a>14.5关注和粉丝逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们的user表里 有一个following字段 里面是个数组 用来存放我们关注的人的ID</span><br><span class="line"><span class="number">2.</span>前端会给我们传过来用户ID 这个用户ID 有可能给是文章本人 或者是自己的 </span><br><span class="line"><span class="number">3.</span>关注就是去查自己表里following字段就好  然后返回个前端 这里就有作者的消息和昵称</span><br><span class="line"><span class="number">4.</span>粉丝就是要去查所有的用户的user表 然后看看他们的following字段里有没有自己 如果有就是自己的粉丝 </span><br><span class="line"><span class="number">5.</span>following字段里会关联到我们user表 这样查出来的时候 才会把你关注人的信息都查出来</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//用户表</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> mongoose = app.mongoose;</span><br><span class="line">  <span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> UserSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">    <span class="comment">// select就是在我们查询数据库的时候不返还给前端的设置</span></span><br><span class="line">    __v: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    email: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    password: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    nickname: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    <span class="comment">// 关注的人字段 这映射我们user表 查的时候就会返回ID相关的所有信息</span></span><br><span class="line">    following: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;User&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//点赞的文章的字段  </span></span><br><span class="line">    likeArticle: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;Article&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//踩的文章的字段  </span></span><br><span class="line">    disArticle: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;Article&#x27;</span> &#125;],</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">  &#125;, &#123; <span class="attr">timestamps</span>: <span class="literal">true</span> &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> mongoose.model(<span class="string">&#x27;User&#x27;</span>, UserSchema);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//关注逻辑 路由</span></span><br><span class="line">router.get(<span class="string">&#x27;/user/:id/following&#x27;</span>, jwt, controller.user.following);</span><br><span class="line"><span class="comment">//controller 文件夹下 user.js文件 following方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="comment">//关注列表</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">following</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">// 只要去查看自己的表里follow字段即可然后 把对应的user表也映射进去</span></span><br><span class="line">    <span class="keyword">const</span> users = <span class="keyword">await</span> ctx.model.User.findById(ctx.params.id).populate(<span class="string">&#x27;following&#x27;</span>);</span><br><span class="line">    <span class="built_in">console</span>.log(users, <span class="string">&#x27;-----关注&#x27;</span>);</span><br><span class="line">    <span class="built_in">this</span>.success(users.following);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//粉丝逻辑 路由</span></span><br><span class="line">router.get(<span class="string">&#x27;/user/:id/followers&#x27;</span>, jwt, controller.user.followers);</span><br><span class="line"><span class="comment">//controller 文件夹下 user.js文件 followers方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 获取关注自己的人</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">followers</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">// 只要去查看自己的表里follow字段即可 这的意思就是 我们去user表里查一下有自己或者文章作者的ID即可 返回是个数组</span></span><br><span class="line">    <span class="keyword">const</span> users = <span class="keyword">await</span> ctx.model.User.find(&#123; <span class="attr">following</span>: ctx.params.id &#125;);</span><br><span class="line">    <span class="built_in">this</span>.success(users);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="15文章点赞和踩逻辑"><a href="#15文章点赞和踩逻辑" class="headerlink" title="15文章点赞和踩逻辑"></a>15文章点赞和踩逻辑</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先文章点赞 和踩 是互斥 也就是你点了赞之后就不能踩了</span><br><span class="line"><span class="number">2.</span>这样我们的用户表里就需要有两个字段likeArticle 和 disArticle 用来记录我们点过赞的文章 和踩过的文章 这里需要ref:Article 把我们的点赞的文章的具体表映射过来</span><br><span class="line"><span class="number">3.</span>然后对应的article表里 要有 like 和 dilike 的两个字段 用来记录该篇文章赞数 和 踩数</span><br><span class="line"><span class="number">4.</span>前端发送文章 ID 我们去我们的user表里的likeArticle字段里去查询 你点没点过赞 </span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//user表</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> mongoose = app.mongoose;</span><br><span class="line">  <span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line">  <span class="keyword">const</span> UserSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">  __v: &#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line"></span><br><span class="line">    email: &#123; <span class="attr">type</span>: <span class="built_in">String</span> , <span class="attr">required</span>:<span class="literal">true</span>  &#125;,</span><br><span class="line">    nickname: &#123; <span class="attr">type</span>: <span class="built_in">String</span> , <span class="attr">required</span>:<span class="literal">false</span>  &#125;,</span><br><span class="line">    avatar: &#123; <span class="attr">type</span>: <span class="built_in">String</span> , <span class="attr">required</span>:<span class="literal">false</span>,<span class="attr">default</span>:<span class="string">&#x27;/user.png&#x27;</span>  &#125;,</span><br><span class="line">    password: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>:<span class="literal">true</span>  ,<span class="attr">select</span>: <span class="literal">false</span>,&#125;,</span><br><span class="line">    following: &#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;User&#x27;</span> &#125;]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//这个用来记录你点过赞的文章 里面映射到 文章的表  </span></span><br><span class="line">    likeArticle:&#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;Article&#x27;</span> &#125;]</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//这个用来记录你点踩过的文章 里面映射到 文章的表  </span></span><br><span class="line">    dislikeArticle:&#123;</span><br><span class="line">      type: [&#123; <span class="attr">type</span>: Schema.Types.ObjectId, <span class="attr">ref</span>: <span class="string">&#x27;Article&#x27;</span> &#125;]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,&#123; <span class="attr">timestamps</span>: <span class="literal">true</span> &#125;);</span><br><span class="line">  <span class="keyword">return</span> mongoose.model(<span class="string">&#x27;User&#x27;</span>, UserSchema);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//acticle表</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">app</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> mongoose = app.mongoose;</span><br><span class="line">  <span class="keyword">const</span> Schema = mongoose.Schema;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">const</span> ArticleSchema = <span class="keyword">new</span> Schema(&#123;</span><br><span class="line">    <span class="comment">// select就是在我们查询数据库的时候不返还给前端的设置</span></span><br><span class="line">    __v: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">select</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    title: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    article: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    article_html: &#123; <span class="attr">type</span>: <span class="built_in">String</span>, <span class="attr">required</span>: <span class="literal">true</span> &#125;,</span><br><span class="line">    views: &#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">required</span>: <span class="literal">false</span> &#125;,</span><br><span class="line">    author: &#123;</span><br><span class="line">      type: Schema.Types.ObjectId,</span><br><span class="line">      ref: <span class="string">&#x27;User&#x27;</span>,</span><br><span class="line">      required: <span class="literal">true</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 文章的赞数</span></span><br><span class="line">    like: &#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">required</span>: <span class="literal">false</span>, <span class="attr">defualt</span>: <span class="number">0</span> &#125;,</span><br><span class="line">    <span class="comment">// 文章的踩数</span></span><br><span class="line">    dilike: &#123; <span class="attr">type</span>: <span class="built_in">Number</span>, <span class="attr">required</span>: <span class="literal">false</span>, <span class="attr">defualt</span>: <span class="number">0</span> &#125;,</span><br><span class="line">    <span class="comment">// 上面两者是互斥的</span></span><br><span class="line">  &#125;, &#123; <span class="attr">timestamps</span>: <span class="literal">true</span> &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> mongoose.model(<span class="string">&#x27;Article&#x27;</span>, ArticleSchema);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="15-1初始化点赞或者踩按钮的状态"><a href="#15-1初始化点赞或者踩按钮的状态" class="headerlink" title="15.1初始化点赞或者踩按钮的状态"></a>15.1初始化点赞或者踩按钮的状态</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>前端会给我传来文章ID  然后我们去查询我们自己的user表</span><br><span class="line"><span class="number">2.</span>然后查找我们的likeArticle字段(赞过的文章) 和dislikeArticle 字段(踩过的文章) </span><br><span class="line"><span class="number">3.</span>根据前端给我们传过来的ID 去查看以上两个字段里有没有 并返回布尔值</span><br><span class="line"><span class="number">4.</span>前端在初始化页面的是时候就去发送这个请求 然后就能更具返还回来的布尔值 去控制你按钮的状态</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> Controller = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">articleStatus</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">const</span> &#123;ctx&#125; = <span class="built_in">this</span></span><br><span class="line">    <span class="comment">//先去找到我们自己的用户表</span></span><br><span class="line">    <span class="keyword">const</span> me = <span class="keyword">await</span> <span class="built_in">this</span>.ctx.model.User.findById(ctx.state.userid);</span><br><span class="line">    <span class="comment">//然后根据前端传过来的ID 去下面两个字段查看有没有 并返回布尔值    </span></span><br><span class="line">    <span class="keyword">let</span> like = !!me.likeArticle.find(<span class="function"><span class="params">id</span>=&gt;</span>id.toString()==ctx.params.id)    </span><br><span class="line">    <span class="keyword">let</span> dislike = !!me.dislikeArticle.find(<span class="function"><span class="params">id</span>=&gt;</span>id.toString()==ctx.params.id)   </span><br><span class="line">    <span class="built_in">this</span>.success(&#123;</span><br><span class="line">      like,dislike</span><br><span class="line">    &#125;) </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="15-1文章的点赞和取消点赞"><a href="#15-1文章的点赞和取消点赞" class="headerlink" title="15.1文章的点赞和取消点赞"></a>15.1文章的点赞和取消点赞</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>前端的点赞和取消点赞 是通过一个状态按钮的状态变化 来控制两个不同类型的请求</span><br><span class="line"><span class="number">2.</span>然后前端给我们过来文章的ID 我们用我们自己的ID去把我自己的用户表查出来</span><br><span class="line"><span class="number">3.</span>然后查出来的表的likeArticle字段 看看有没有前端传过来的文章ID 如果没有 我就把前端添加的ID添加进去</span><br><span class="line"><span class="number">4.</span>然后同时把我们文章表的like字段的数量更新一下</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由  文章点赞</span></span><br><span class="line">router.put(<span class="string">&#x27;/user/likeArticle/:id&#x27;</span>, jwt, controller.user.likeArticle);</span><br><span class="line"><span class="comment">//controller文件夹下 user.js likeArticle方法</span></span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 文章点赞</span></span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">likeArticle</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">     <span class="comment">//先把我们自己的表查出来</span></span><br><span class="line">    <span class="keyword">const</span> me = <span class="keyword">await</span> ctx.model.User.findById(ctx.state.userid);</span><br><span class="line">    <span class="comment">//然后看看我们likeArticle字段里有没有该片文章的ID 如果没有我们就添加进去  </span></span><br><span class="line">    <span class="keyword">if</span> (!me.likeArticle.find(<span class="function"><span class="params">id</span> =&gt;</span> id.toString() === ctx.params.id)) &#123;</span><br><span class="line">      me.likeArticle.push(ctx.params.id);</span><br><span class="line">      <span class="comment">//然后保存  </span></span><br><span class="line">      me.save();</span><br><span class="line">      <span class="comment">//然后把我们的article表的like字段喜欢次数更新  </span></span><br><span class="line">      <span class="keyword">await</span> ctx.model.Article.findByIdAndUpdate(ctx.params.id, &#123; <span class="attr">$inc</span>: &#123; <span class="attr">like</span>: <span class="number">1</span> &#125; &#125;);</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">this</span>.message(<span class="string">&#x27;点赞成功&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由 取消点赞</span></span><br><span class="line">router.delete(<span class="string">&#x27;/user/likeArticle/:id&#x27;</span>, jwt, controller.user.cancellikeArticle);</span><br><span class="line"><span class="meta">&#x27;use strict&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> md5 = <span class="built_in">require</span>(<span class="string">&#x27;md5&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> BaseController = <span class="built_in">require</span>(<span class="string">&#x27;./base&#x27;</span>);</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">BaseController</span> </span>&#123;</span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">cancellikeArticle</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; ctx &#125; = <span class="built_in">this</span>;</span><br><span class="line">    <span class="comment">//还是去查我们的自己表    </span></span><br><span class="line">    <span class="keyword">const</span> me = <span class="keyword">await</span> ctx.model.User.findById(ctx.state.userid);</span><br><span class="line">    <span class="comment">//然后去查看我们的这个字段里有没有该篇文章的ID     </span></span><br><span class="line">    <span class="keyword">const</span> index = me.likeArticle.map(<span class="function"><span class="params">id</span> =&gt;</span> id.toString().indexOf(ctx.params.id));</span><br><span class="line">    <span class="comment">//如果找到了 我们就把他从likeArticle的字段里删除    </span></span><br><span class="line">    <span class="keyword">if</span> (index &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">      me.likeArticle.splice(index, <span class="number">1</span>);</span><br><span class="line">      me.save();</span><br><span class="line">      <span class="comment">//然后让我们的like -1更新  </span></span><br><span class="line">      <span class="keyword">await</span> ctx.model.Article.findByIdAndUpdate(ctx.params.id, &#123; <span class="attr">$inc</span>: &#123; <span class="attr">like</span>: -<span class="number">1</span> &#125; &#125;);</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">this</span>.message(<span class="string">&#x27;取消点赞成功&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(ctx);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>SSR 渲染 之后端</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/14/%E5%90%8E%E7%AB%AF/">http://example.com/2020/04/14/%E5%90%8E%E7%AB%AF/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-14</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93/">服务端渲染</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/14/%E5%89%8D%E7%AB%AF/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SSR 渲染 之前端</div></div></a></div><div class="next-post pull-right"><a href="/2020/04/13/ssr%20nuxt/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">SSR 之 服务端渲染</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/14/前端/" title="SSR 渲染 之前端"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-14</div><div class="title">SSR 渲染 之前端</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">1.初始化</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84%E7%9B%AE%E5%BD%95"><span class="toc-text">2.项目结构目录</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E8%B7%AF%E7%94%B1%E5%92%8Ccontroller%E5%86%99%E6%B3%95"><span class="toc-text">3.路由和controller写法</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E7%BB%99%E9%82%AE%E7%AE%B1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-text">4.给邮箱发送验证码</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-text">5.图片验证码</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%90%8E%E7%AB%AF%E6%B3%A8%E5%86%8C%E9%80%BB%E8%BE%91"><span class="toc-text">6.后端注册逻辑</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE"><span class="toc-text">7.数据库配置</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-mongoose%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">8.mongoose的使用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-jwt%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95token%E6%9D%83%E9%99%90"><span class="toc-text">9.jwt后端登录token权限</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E8%A7%A3%E5%AF%86token"><span class="toc-text">10.解密token</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#11-%E6%96%B0%E5%BB%BA%E6%96%87%E7%AB%A0"><span class="toc-text">11.新建文章</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#12-article%E8%A1%A8"><span class="toc-text">12.article表</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#13-%E8%8E%B7%E5%8F%96%E6%96%87%E7%AB%A0%E5%88%97%E8%A1%A8"><span class="toc-text">13.获取文章列表</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#14-%E6%96%87%E7%AB%A0%E8%AF%A6%E6%83%85"><span class="toc-text">14.文章详情</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#14-1-%E6%9F%A5%E8%AF%A2%E6%96%87%E7%AB%A0%E8%AF%A6%E6%83%85"><span class="toc-text">14.1 查询文章详情</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-2%E5%85%B3%E6%B3%A8%E8%AF%A5%E5%93%A6%E7%89%87%E6%96%87%E7%AB%A0%E4%BD%9C%E8%80%85%E9%80%BB%E8%BE%91"><span class="toc-text">14.2关注该哦片文章作者逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-3%E5%85%B3%E6%B3%A8%E9%80%BB%E8%BE%91"><span class="toc-text">14.3关注逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-4%E5%8F%96%E6%B6%88%E5%85%B3%E6%B3%A8%E9%80%BB%E8%BE%91"><span class="toc-text">14.4取消关注逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-5%E5%85%B3%E6%B3%A8%E5%92%8C%E7%B2%89%E4%B8%9D%E9%80%BB%E8%BE%91"><span class="toc-text">14.5关注和粉丝逻辑</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#15%E6%96%87%E7%AB%A0%E7%82%B9%E8%B5%9E%E5%92%8C%E8%B8%A9%E9%80%BB%E8%BE%91"><span class="toc-text">15文章点赞和踩逻辑</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#15-1%E5%88%9D%E5%A7%8B%E5%8C%96%E7%82%B9%E8%B5%9E%E6%88%96%E8%80%85%E8%B8%A9%E6%8C%89%E9%92%AE%E7%9A%84%E7%8A%B6%E6%80%81"><span class="toc-text">15.1初始化点赞或者踩按钮的状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15-1%E6%96%87%E7%AB%A0%E7%9A%84%E7%82%B9%E8%B5%9E%E5%92%8C%E5%8F%96%E6%B6%88%E7%82%B9%E8%B5%9E"><span class="toc-text">15.1文章的点赞和取消点赞</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>